export default {
	globalData: {
		statusBarHeight: 0,
		blackBarHeight: 0,
		navHeight: 0,
		prx:2,//px和rpx的换算比例
		screenHeight: 0, //屏幕高度
		scrollAllHeight: 0, // 去除导航栏的滚动容器
		scrollHeight: 0, //去除导航栏和tabbar的滚动容器
		share_id: 0,
		cid: 0, // 系统客户ID/微信用户ID
		uid: 1, // 平台
		agent_cid:0,//分销商cid
		openid: '',
		is_login: 0,
		is_member: 0,
		login_state: 1,
		gzhid: '', //公众号点击绑定小程序带的参数
		session_key: '',
		unionid: '',
		user_info: [],
		account: '',
		scene: '',//扫小程序码接受的参数
		gz: '0px',
		version: '2.1.2',
		api_root: 'https://item.techolic.cn/hjf/api/', // api地址
		upload_root: 'https://item.techolic.cn/hjf/person/upload_file', //文件上传地址
		index_login:0,
		phone:'',
	},

	methods: {
		//打印信息
		print:function(data){
			console.log(JSON.parse(JSON.stringify(data)));
		},
		// 获取iphonex以上机型底部的小黑条的高度
		getBlackBar() {
			let that = this;
			uni.getSystemInfo({
				success: res => {
					console.log(res);
					let screenHeight = res.screenHeight;
					let bottom = res.safeArea.bottom;
					let blackBarHeight = screenHeight - bottom;
					that.globalData.blackBarHeight = blackBarHeight;
				}
			})
		},

		// 自定义导航栏
		getPhoneInfo() {
			let that = this;
			let menuButtonObject = uni.getMenuButtonBoundingClientRect(); //获取胶南的信息 
			console.log(menuButtonObject);
			uni.getSystemInfo({
				success: function(res) {
					let statusBarHeight = res.statusBarHeight; //状态栏的高度
					let navTop = menuButtonObject.top; //胶南距离顶部的高度
					let navHeight = 2 * navTop - statusBarHeight + menuButtonObject.height;
					// console.log(navHeight);
					that.globalData.navTop = navTop;
					that.globalData.statusBarHeight = statusBarHeight;
					that.globalData.navHeight = navHeight; //导航栏高度
					that.globalData.screenHeight = res.screenHeight; //屏幕高度
					that.globalData.menuButtonObject = menuButtonObject; //胶囊
					// console.log(that.globalData.blackBarHeight)
					that.globalData.scrollAllHeight = res.screenHeight - that.globalData
						.navHeight; //去除导航栏的滚动容器
					that.globalData.scrollHeight = res.screenHeight - that.globalData.navHeight -
						44; //去除导航栏和tabbar的滚动容器
					//px和rpx的换算比例
					that.globalData.prx =  750 /res.screenWidth 

				},
				fail: (err) => {
					console.log(err)
				}
			})
		},
		/**
		 * get请求
		 */
		_get: function(url, data, success, fail, complete, check_login) {
			uni.showNavigationBarLoading(); //导航栏顶部加载
			let app = this;
			// 构造请求参数
			data = data || {};
			data.cid = app.globalData.cid;
			data.uid = app.globalData.uid;
			data.openid = app.globalData.openid;
			data.version = app.globalData.version;
			
			console.log('get:  ' + url);
			console.log(data);
			// 构造get请求
			let request = function() {
				uni.request({
					url: app.globalData.api_root + url,
					header: {
						'content-type': 'application/json'
					},
					data: data,
					success: function(res) {
						console.log('get:  ' + url + ' result: ↓↓↓↓↓↓↓↓');
						console.log(JSON.parse(JSON.stringify(res.data)));
						if (res.statusCode !== 200 || typeof res.data !== 'object') {
							app.showError('网络请求出错');
							return false;
						}
						if (res.data.code === -1) {
							// 登录态失效, 重新登录
							uni.hideNavigationBarLoading(); //关闭导航栏加载
							//app.doLogin();
						} else if (res.data.code === 0) {
							app.showError(res.data.msg);
							return false;
						} else {
							success && success(res.data);
						}
					},
					fail: function(res) {
						app.showError(res.errMsg, function() {
							fail && fail(res);
						});
					},
					complete: function(res) {
						uni.hideNavigationBarLoading();
						complete && complete(res);
					},
				});
			};
			// 判断是否需要验证登录
			check_login ? app.doLogin(request) : request();
		},

		/**
		 * post提交
		 */
		_post_form: function(url, data, success, fail, complete) {
			uni.showNavigationBarLoading();
			let app = this;
			data.cid = app.globalData.cid;
			data.uid = app.globalData.uid;
			data.openid = app.globalData.openid;
			data.version = app.globalData.version;
			
			console.log('post:  ' + url);
			console.log(data);
			uni.request({
				url: app.globalData.api_root + url,
				header: {
					'Content-Type': 'application/x-www-form-urlencoded',
				},
				method: 'POST',
				data: data,
				success: function(res) {
					console.log('post:  ' + url + ' result: ↓↓↓↓↓↓↓↓');
					console.log(JSON.parse(JSON.stringify(res.data)));
					if (res.statusCode !== 200 || typeof res.data !== 'object') {
						app.showError('网络请求出错');
						return false;
					}
					if (res.data.code === -1) {
						// 登录态失效, 重新登录
						app.doLogin(function() {
							app._post_form(url, data, success, fail);
						});
						return false;
					} else if (res.data.code === 0) {
						app.showError(res.data.msg, function() {
							fail && fail(res);
						});
						return false;
					}
					success && success(res.data);
				},
				fail: function(res) {
					app.showError(res.errMsg, function() {
						fail && fail(res);
					});
				},
				complete: function(res) {
					//wx.hideLoading();
					uni.hideNavigationBarLoading();
					complete && complete(res);
				}
			});
		},
		/**
		 * 显示成功提示框
		 */
		showSuccess: function(msg, callback) {
			uni.showToast({
				title: msg,
				icon: 'success',
				success: function() {
					callback && (setTimeout(function() {
						callback();
					}, 1500));
				}
			});
		},
		/**
		 * 显示失败提示框
		 */
		showError: function(msg, callback) {
			uni.showModal({
				title: '友情提示',
				content: msg,
				showCancel: false,
				success: function(res) {
					// callback && (setTimeout(function() {
					//   callback();
					// }, 1500));
					callback && callback();
				}
			});
		},
		/**
		 * 显示长信息提示框
		 */
		showMsg: function(msg, callback) {
			uni.showToast({
				title: msg,
				icon: 'none',
				success: function() {
					callback && (setTimeout(function() {
						callback();
						uni.hideLoading();
					}, 3000));
				}
			});
		},
		//登录 获取openid
		login: function(callback) {
			console.log('登录ing...')
			var _this = this;
			const app = getApp();
			if (app.globalData.openid == '') {
				uni.login({
					success: function(res) {
						console.log(res.code);
						if (res.code) {
							_this._post_form('wx/login', {
								code: res.code,
								pid: app.globalData.pid,
								scene: app.globalData.scene,
								gzhid: app.globalData.gzhid,
							}, function(result) {
								console.log(result);
								var openid = result.openid;
								var session_key = result.session_key;
								var unionid = result.unionid;
								app.globalData.openid = openid;
								app.globalData.unionid = unionid;
								app.globalData.session_key = session_key;
								app.globalData.user_info = result.userInfo;
								//app.globalData.account = result.account;
								app.globalData.cid = result.cid;
								app.globalData.phone = result.phone;
								app.globalData.is_vip = result.is_vip;
								app.globalData.vip_time = result.vip_time;
								//app.globalData.is_member = result.is_member;
								//app.globalData.is_login = result.is_login;
								if (result.cid == 0) {
									_this.is_phone();
								} else {
									console.log('授权登录成功,globalData:::')
									console.log(app.globalData)
								}
								//绑定公众号提示
								if (result.bind_gzh == 1) {
									_this.showError(result.bind_gzh_result);
								}
								//绑定厂家
								/*if (result.bind_factor == 1) {
									_this.showError(result.bind_factor_result);
								}*/
								callback && callback();
								// if (result.uid > 0) {
								// 	if (unionid != '' && typeof(unionid) != 'undefined' && result.unionid_status) {
								// 		_this.unionid();
								// 	}
								// }
							});
						} else {
							console.log('获取用户登录态失败！' + res.errMsg)
						}
					}
				});
			} else {
				_this.is_phone();
				callback && callback();
			}
		},
		//判断是否绑定了手机号
		is_phone: function() {
			var _this = this; 
			const app = getApp();
			console.log('openid不为空，globalData：：：');
			console.log(app.globalData);
			//if (app.globalData.user_info.length <= 0) {
			if (app.globalData.phone == '' || app.globalData.cid == '') {
				if(_this.globalData.scene || _this.globalData.gzhid){//若是从绑定公众号或小程序进来的则直接跳转授权页面
					uni.reLaunch({
						url: "/pages/doLogin/doLogin"
					})
				}else{
					// 保存当前页面
					let pages = getCurrentPages();
					console.log(pages);
					if (pages.length) {
						let currentPage = pages[pages.length - 1];
						console.log(typeof currentPage);
						console.log(currentPage);
						console.log(currentPage.route);
						if (("pages/doLogin/doLogin" != currentPage.route && "pages/login/login" != currentPage.route && "pages/index/index" != currentPage.route) || app.globalData.index_login == 1) {
							uni.reLaunch({
								url: "/pages/doLogin/doLogin"
							})
						}
					}
				}
			}
		},
		//获取个人信息
		get_user_info: function(callback) {
			let _this = this;
			const app = getApp();
			let openid = _this.globalData.openid;
			app._get('wx/get_user_info', {
				openid,
				gzhid: app.globalData.gzhid,
				scene: app.globalData.scene,
			}, function(res) {
				if (res.cid == 0) {
					app.is_phone();
					callback && callback();
				} else {
					app.globalData.unionid = res.unionid;
					app.globalData.session_key = session_key;
					app.globalData.user_info = result.userInfo;
					app.globalData.cid = result.cid;
					console.log('授权登录成功,globalData:::')
					console.log(app.globalData)
					if (res.bind_gzh == 1) {
						_this.showError(res.bind_gzh_result);
					}
					//绑定厂家
					if (res.bind_factor == 1) {
						_this.showError(res.bind_factor_result);
					}
					callback && callback();
				}
			})
		},
		//获取unionid
		unionid: function() {
			var _this = this;
			const app = getApp();
			console.log('3333444  unnionid')
			_this._post_form('wx/unionid', {
				// unionid: _this.data.unionid,
				unionid: app.globalData.unionid,

			}, function(result) {});
		},
		//获取用户信息
		get_userinfo: function() {
			var _this = this;
			const app = getApp();
			_this._get('user/get_userinfo', {}, function(result) {
				app.globalData.is_vip = result.is_vip;
			});
		},
		//获取openid对应的用户信息 
		user: function(openid) {
			var that = this;
			const app = getApp();
			uni.request({
				url: 'https://es.hivetech.cn/media/Api/Api/user_add',
				data: {
					openid: openid,
					username: openid,
					password: '1231231231',
					email: '123@qq.com',
					mobile: '1897861123',
				},
				method: 'POST',
				header: {
					'content-type': 'application/x-www-form-urlencoded'
				},
				success: function(res) {
					app.globalData.uid = res.data.uid;
					app.globalData.wx_userinfo = res.data.wx_userinfo;
					app.globalData.unionid = res.data.unionid;
					uni.setStorage({
						key: "uid",
						data: res.data.uid
					})
					uni.setStorage({
						key: "unionid",
						data: res.data.unionid
					})
				}
			})
		},
		/**
		 * 执行用户登录
		 */
		doLogin: function() {
			// 保存当前页面
			let pages = getCurrentPages();
			if (pages.length) {
				let currentPage = pages[pages.length - 1];
				"pages/doLogin/doLogin" != currentPage.route &&
					uni.setStorageSync("currentPage", currentPage);
			}
		},
		/**
		 * 对象转URL
		 */
		urlEncode: function urlencode(data) {
			var _result = [];
			for (var key in data) {
				var value = data[key];
				if (value.constructor == Array) {
					value.forEach(function(_value) {
						_result.push(key + "=" + _value);
					});
				} else {
					_result.push(key + '=' + value);
				}
			}
			return _result.join('&');
		},
	}
}
